using Application.Abstractions.Authentication;
using Application.Abstractions.Data;
using Domain.Users;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
using SharedKernel;

namespace Infrastructure.Database.DataSeeding.DataSeeders;

public class UserSeeder(
    IApplicationDbContext context,
    IPasswordHasher passwordHasher,
    IDateTimeProvider dateTimeProvider,
    ILogger<UserSeeder> logger)
    : IDataSeeder
{
    public async Task SeedAsync(CancellationToken cancellationToken = default)
    {
        if (await context.Users.AnyAsync(cancellationToken))
        {
            logger.LogInformation("用户数据已存在，跳过种子");
            return;
        }

        var users = new List<User>
        {
            new("admin@example.com", "管理员", "用户", passwordHasher.Hash("Admin123!"), dateTimeProvider),
            new("john.doe@example.com", "John", "Doe", passwordHasher.Hash("User123!"), dateTimeProvider),
            new("jane.smith@example.com", "Jane", "Smith", passwordHasher.Hash("User123!"), dateTimeProvider),
            new("test@example.com", "测试", "用户", passwordHasher.Hash("Test123!"), dateTimeProvider)
        };

        await context.Users.AddRangeAsync(users, cancellationToken);
        await context.SaveChangesAsync(cancellationToken);
        
        logger.LogInformation("已添加 {Count} 个用户种子数据", users.Count);
    }
}